package com.adinall.dsp.core.poi.excel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.List;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.adinall.dsp.core.utils.CheckUtils;

public abstract class Write extends Excel {

    private String filePath;
    private String fileName;

    private XSSFWorkbook workbook;

    public Write(String filePath, String fileName) {

        this.filePath = filePath;
        this.fileName = fileName;
        workbook = new XSSFWorkbook();
    }

    public XSSFWorkbook getWorkbook() {
        return workbook;
    }

    public XSSFSheet createSheet(String sheetName) {
        return workbook.createSheet(sheetName);
    }

    public void setValue(XSSFSheet sheet, int rownum, int columnIndex, String value, CellStyle style) {

        XSSFCell cell = getCell(sheet, rownum, columnIndex);
        cell.setCellValue(value);

        if (!CheckUtils.isNull(style)) {
            cell.setCellStyle(style);
        }
    }

    public void setValue(XSSFSheet sheet, int rownum, int columnIndex, String value) {
        setValue(sheet, rownum, columnIndex, value, null);
    }

    public void setValue(XSSFSheet sheet, int rownum, int columnIndex, boolean value) {
        XSSFCell cell = getCell(sheet, rownum, columnIndex);
        cell.setCellValue(value);
    }

    public void setValue(XSSFSheet sheet, int rownum, int columnIndex, int value, CellStyle style) {

        XSSFCell cell = getCell(sheet, rownum, columnIndex);
        cell.setCellValue(value);

        if (!CheckUtils.isNull(style)) {
            cell.setCellStyle(style);
        }
    }

    public void setValue(XSSFSheet sheet, int rownum, int columnIndex, int value) {
        setValue(sheet, rownum, columnIndex, value, null);
    }

    public void setValue(XSSFSheet sheet, int rownum, int columnIndex, double value, CellStyle style) {

        XSSFCell cell = getCell(sheet, rownum, columnIndex);
        cell.setCellValue(value);

        if (!CheckUtils.isNull(style)) {
            cell.setCellStyle(style);
        }
    }

    public void setValue(XSSFSheet sheet, int rownum, int columnIndex, double value) {
        setValue(sheet, rownum, columnIndex, value, null);
    }

    public void setValue(XSSFSheet sheet, int rownum, int columnIndex, BigDecimal value) {

        XSSFCell cell = getCell(sheet, rownum, columnIndex);

        if (CheckUtils.isNull(value)) {
            cell.setCellValue(0.00D);
        } else {
            cell.setCellValue(value.doubleValue());
        }
    }

    public void setValue(XSSFSheet sheet, int rownum, int columnIndex, List data) {

        if (data == null || data.isEmpty()) {
            return;
        }

        for (Object obj : data) {

            if (obj instanceof String) {

                setValue(sheet, rownum, columnIndex++, String.valueOf(obj));

            } else if (obj instanceof Number) {

                setValue(sheet, rownum, columnIndex++, Double.parseDouble(String.valueOf(obj)));

            } else if (obj instanceof Boolean) {

                setValue(sheet, rownum, columnIndex++, Boolean.parseBoolean(String.valueOf(obj)));
            } else {
                throw new RuntimeException("不支持的类型");
            }
        }
    }

    public void setColumnWidth(XSSFSheet sheet, int columnIndex, int width) {
        sheet.setColumnWidth(columnIndex, width);
    }

    public void setHeight(XSSFSheet sheet, int rowStartIdx, int rowEndIdx, short height) {

        for (int i = rowStartIdx; i < rowEndIdx; i++) {
            getRow(sheet, i).setHeight(height);
        }
    }

    public void setHeightInPoints(XSSFSheet sheet, int firstRowIdx, int lastRowIdx, float height) {

        for (int i = firstRowIdx; i < lastRowIdx; i++) {
            getRow(sheet, i).setHeightInPoints(height);
        }
    }

    public void addMergedRegion(XSSFSheet sheet, int firstRow, int lastRow, int firstCol, int lastCol) {
        sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol, lastCol));
    }

    public void write() {

        OutputStream os = null;
        File f = new File(filePath);

        if (!f.exists()) {
            f.mkdirs();
        }

        try {
            os = new FileOutputStream(filePath + fileName);
            workbook.write(os);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            if (os != null) {
                try {
                    os.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            closeWorkbook();
        }
    }

    public void closeWorkbook() {

        if (workbook != null) {
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
